package com.yztc.xiami_app.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.gson.Gson;
import com.yztc.xiami_app.R;
import com.yztc.xiami_app.bean.classify.ClassifiBean;
import com.yztc.xiami_app.http.OkHttpUtils;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class DetailActivity extends AppCompatActivity {
    private String urll = "http://m.ttcj.tv/xml/appcategoryjson.htm";
    private String title;
    private String htmlStr;
    private Toolbar detailToolbar;
    private WebView detailWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        initData();
        initWebViewData();
    }

    private void initWebViewData() {
        OkHttpUtils.doGETRequest(urll, new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //解析数据
                String result = OkHttpUtils.getStringFromResponse(response);
                //json解析
                Gson gson = new Gson();
                ClassifiBean classifiBean = gson.fromJson(result, ClassifiBean.class);
                List<ClassifiBean.CatelistBean> catelist = classifiBean.getCatelist();
                for (int i = 0; i < catelist.size(); i++) {
                    ClassifiBean.CatelistBean catelistBean = catelist.get(i);
                    List<ClassifiBean.CatelistBean.Category2Bean> category2 = catelistBean.getCategory2();
                    for (int j = 0; j < category2.size(); j++) {
                        htmlStr = category2.get(j).getUrl();
                        Log.e("tag", htmlStr);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                initWebView(htmlStr);
                            }
                        });
                    }
                }
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        title = intent.getStringExtra(title);
    }

    private void initWebView(String url) {
        detailWebView = (WebView) findViewById(R.id.detail_webview);
        detailWebView.loadUrl(url);
        detailWebView.getSettings().setJavaScriptEnabled(true);
        detailWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }



}
